﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Skywave.Drawing.Printing;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using Skywave.Windows.Forms.RuntimeDesign;

namespace Skywave.Windows.Forms
{
    public partial class AdvancedPrintDocumentSettings : UserControl
    {
        public AdvancedPrintDocumentSettings()
        {
            InitializeComponent();
            frmH.Text += Properties.Resources.AdvancedPrintDocumentSettings_Header;
            frmF.Text += Properties.Resources.AdvancedPrintDocumentSettings_Footer;
        }
        public event EventHandler HeaderClick;
        public event EventHandler FooterClick;
        private SimpleFormDesignerForm frmH = new SimpleFormDesignerForm();
        private SimpleFormDesignerForm frmF = new SimpleFormDesignerForm();

        private AdvancedPrintDocument mAdvancedPrintDocument;
        public AdvancedPrintDocument Document
        {
            get { return mAdvancedPrintDocument; }
            set
            {
                mAdvancedPrintDocument = value;
                RefreshByAPD();
            }
        }

        private bool mAutoHeader = true;
        [DefaultValue(true)]
        public bool AutoHeader
        {
            get { return mAutoHeader; }
            set { mAutoHeader = value; }
        }

        private bool mAutoFooter = true;
        [DefaultValue(true)]
        public bool AutoFooter
        {
            get { return mAutoFooter; }
            set { mAutoFooter = value; }
        }

        private void RefreshByAPD()
        {
            frmH.FormDesigner.InitializeMe();
            frmF.FormDesigner.InitializeMe();
            if (mAdvancedPrintDocument == null) { Enabled = false; return; }
            if (mAdvancedPrintDocument.Header != null)
                frmH.FormDesigner = mAdvancedPrintDocument.Header;
            if (mAdvancedPrintDocument.Footer != null)
                frmF.FormDesigner = mAdvancedPrintDocument.Footer;
            Enabled = true;
            chkFooter.Checked = mAdvancedPrintDocument.PrintFooter;
            chkHeader.Checked = mAdvancedPrintDocument.PrintHeader;
            chkTileH.Checked = mAdvancedPrintDocument.TileHorizontally;
            chkTileV.Checked = mAdvancedPrintDocument.TileVertically;
            txtDistance.Text = mAdvancedPrintDocument.BetweenBorderSize.ToString();
            switch (mAdvancedPrintDocument.HeaderPosition)
            {
                case ContentAlignment.TopCenter:
                    comboBoxHP.SelectedIndex = 1;
                    break;
                case ContentAlignment.TopLeft:
                    comboBoxHP.SelectedIndex = 0;
                    break;
                case ContentAlignment.TopRight:
                    comboBoxHP.SelectedIndex = 2;
                    break;
                default:
                    comboBoxHP.SelectedIndex = 1;
                    break;
            }
            switch (mAdvancedPrintDocument.FooterPosition)
            {
                case ContentAlignment.TopCenter:
                    comboBoxFP.SelectedIndex = 1;
                    break;
                case ContentAlignment.TopLeft:
                    comboBoxFP.SelectedIndex = 0;
                    break;
                case ContentAlignment.TopRight:
                    comboBoxFP.SelectedIndex = 2;
                    break;
                default:
                    comboBoxFP.SelectedIndex = 1;
                    break;
            }
        }

        private Skywave.ComponentModel.ProfilesHolder mProfilesHolderAdvanced;
        [Browsable(false)]
        public Skywave.ComponentModel.ProfilesHolder ProfilesHolderAdvanced
        {
            get { return mProfilesHolderAdvanced; }
            set
            {
                mProfilesHolderAdvanced = value;
                profilesManager1.InitializeMe(mProfilesHolderAdvanced);
            }
        }

        [Browsable(false)]
        public Skywave.ComponentModel.ProfilesHolder ProfilesHolderHeader
        {
            get { return frmH.ProfilesHolder; }
            set
            {
                frmH.ProfilesHolder = value;
                mAdvancedPrintDocument.Header = frmH.FormDesigner;
            }
        }

        [Browsable(false)]
        public Skywave.ComponentModel.ProfilesHolder ProfilesHolderFooter
        {
            get { return frmF.ProfilesHolder; }
            set
            {
                frmF.ProfilesHolder = value;
                mAdvancedPrintDocument.Footer = frmF.FormDesigner;
            }
        }

        public void InitializeMe(string storePath)
        {
            if (storePath == "") return;
            ProfilesHolderAdvanced = new Skywave.ComponentModel.ProfilesHolder(storePath + @"\Advanced");
            ProfilesHolderHeader = new Skywave.ComponentModel.ProfilesHolder(storePath + @"\Header");
            ProfilesHolderFooter = new Skywave.ComponentModel.ProfilesHolder(storePath + @"\Footer");
        }
        //private string mProfilesPath;
        //public string ProfilesPath
        //{
        //    get { return profilesManager1.ProfilesPath; }
        //    set
        //    {
        //        if (value == null || value == "") return;
        //        mProfilesPath = value;
        //        profilesManager1.ProfilesPath = value + @"\Advanced";
        //        if (mAutoHeader)
        //        {
        //            frmH.ProfilesPath = mProfilesPath + @"\Header";
        //            mAdvancedPrintDocument.Header = frmH.FormDesigner;
        //        }
        //        if (mAutoFooter)
        //        {
        //            frmF.ProfilesPath = mProfilesPath + @"\Footer";
        //            mAdvancedPrintDocument.Footer = frmF.FormDesigner;
        //        }
        //    }
        //}

        private void chkHeader_CheckedChanged(object sender, EventArgs e)
        {
            if (mAdvancedPrintDocument == null) return;
            mAdvancedPrintDocument.PrintHeader = chkHeader.Checked;
            cmdHeader.Enabled = chkHeader.Checked;
            comboBoxHP.Enabled = chkHeader.Checked;
        }

        private void chkFooter_CheckedChanged(object sender, EventArgs e)
        {
            if (mAdvancedPrintDocument == null) return;
            mAdvancedPrintDocument.PrintFooter = chkFooter.Checked;
            cmdFooter.Enabled = chkFooter.Checked;
            comboBoxFP.Enabled = chkFooter.Checked;
        }

        private void chkTileH_CheckedChanged(object sender, EventArgs e)
        {
            if (mAdvancedPrintDocument == null) return;
            mAdvancedPrintDocument.TileHorizontally = chkTileH.Checked;
            lblDistance.Enabled = (chkTileH.Checked || chkTileV.Checked);
            txtDistance.Enabled = (chkTileH.Checked || chkTileV.Checked);
        }

        private void chkTileV_CheckedChanged(object sender, EventArgs e)
        {
            if (mAdvancedPrintDocument == null) return;
            mAdvancedPrintDocument.TileVertically = chkTileV.Checked;
            lblDistance.Enabled = (chkTileH.Checked || chkTileV.Checked);
            txtDistance.Enabled = (chkTileH.Checked || chkTileV.Checked);
        }

        private int MaInt(string str)
        {
            return (str == "") ? 0 : int.Parse(str);
        }
        private void txtDistance_TextChanged(object sender, EventArgs e)
        {
            if (mAdvancedPrintDocument == null) return;
            mAdvancedPrintDocument.BetweenBorderSize = MaInt(txtDistance.Text);
        }

        private void cmdHeader_Click(object sender, EventArgs e)
        {
            if (HeaderClick != null) HeaderClick(this, e);
            if (!mAutoHeader) return;
            frmH.FormDesigner.InitializeMe();
            frmH.FormDesigner.ChoosedPrintDocument = mAdvancedPrintDocument.Document;
            frmH.FormDesigner.CopyFrom(mAdvancedPrintDocument.Header);
            foreach (FormDesignerField fdf in mAdvancedPrintDocument.Header.Fields)
            {
                frmH.FormDesigner.AddField(fdf);
            }
            if (frmH.ShowDialog(this.ParentForm) == DialogResult.OK)
                mAdvancedPrintDocument.Header.CopyFrom(frmH.FormDesigner);
        }

        private void cmdFooter_Click(object sender, EventArgs e)
        {
            if (FooterClick != null) FooterClick(this, e);
            if (!mAutoFooter) return;
            frmF.FormDesigner.InitializeMe();
            frmF.FormDesigner.ChoosedPrintDocument = mAdvancedPrintDocument.Document;
            frmF.FormDesigner.CopyFrom(mAdvancedPrintDocument.Footer);
            foreach (FormDesignerField fdf in mAdvancedPrintDocument.Footer.Fields)
            {
                frmF.FormDesigner.AddField(fdf);
            }
            if (frmF.ShowDialog(this.ParentForm) == DialogResult.OK)
                mAdvancedPrintDocument.Footer.CopyFrom(frmF.FormDesigner);
        }

        IFormatter formatter = new BinaryFormatter();
        private void profilesManager1_Serialize(object sender, ProfilesManagerProfileEventArgs e)
        {
            formatter.Serialize(e.Content, chkHeader.Checked);
            formatter.Serialize(e.Content, chkFooter.Checked);
            formatter.Serialize(e.Content, chkTileH.Checked);
            formatter.Serialize(e.Content, chkTileV.Checked);
            formatter.Serialize(e.Content, MaInt(txtDistance.Text));
            formatter.Serialize(e.Content, comboBoxHP.SelectedIndex);
            formatter.Serialize(e.Content, comboBoxFP.SelectedIndex);
        }

        private void profilesManager1_Deserialize(object sender, ProfilesManagerProfileEventArgs e)
        {
            chkHeader.Checked = (bool)formatter.Deserialize(e.Content);
            chkFooter.Checked = (bool)formatter.Deserialize(e.Content);
            chkTileH.Checked = (bool)formatter.Deserialize(e.Content);
            chkTileV.Checked = (bool)formatter.Deserialize(e.Content);
            txtDistance.Text = ((int)formatter.Deserialize(e.Content)).ToString();
            try
            {
                comboBoxHP.SelectedIndex = (int)formatter.Deserialize(e.Content);
                comboBoxFP.SelectedIndex = (int)formatter.Deserialize(e.Content);
            }
            catch (Exception)
            {
            }
        }

        private void comboBoxHP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mAdvancedPrintDocument == null) return;
            ContentAlignment ca1;
            switch (comboBoxHP.SelectedIndex)
            {
                case 0:
                    ca1 = ContentAlignment.TopLeft;
                    break;
                case 1:
                    ca1 = ContentAlignment.TopCenter;
                    break;
                case 2:
                    ca1 = ContentAlignment.TopRight;
                    break;
                default:
                    ca1 = ContentAlignment.TopCenter;
                    break;
            }
            mAdvancedPrintDocument.HeaderPosition = ca1;
        }

        private void comboBoxFP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mAdvancedPrintDocument == null) return;
            ContentAlignment ca1;
            switch (comboBoxFP.SelectedIndex)
            {
                case 0:
                    ca1 = ContentAlignment.TopLeft;
                    break;
                case 1:
                    ca1 = ContentAlignment.TopCenter;
                    break;
                case 2:
                    ca1 = ContentAlignment.TopRight;
                    break;
                default:
                    ca1 = ContentAlignment.TopCenter;
                    break;
            }
            mAdvancedPrintDocument.FooterPosition = ca1;
        }
    }
}
